
import java.awt.Desktop;
import java.awt.List;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.filechooser.FileSystemView;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;

/**
 *
 * @author Hoa
 */
public class NewJFrame extends javax.swing.JFrame {

    DefaultTableModel modelTable;
    int clickCount;
//    Deque<String> forward=new LinkedList<String>(){
//
//        @Override
//        public void addFirst(String e) {
//            if(this.size()>10){
//                super.removeLast();
//            }
//            super.addFirst(e); //To change body of generated methods, choose Tools | Templates.
//        }
//        
//    };

    public NewJFrame() {
        initComponents();

        //getDrive();
        setTree();
        setTable();
    }

    private void setTree() {
        changeIconLeaf();
        getDriveName();
    }

    private void changeIconLeaf() {
        DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
        renderer.setLeafIcon(renderer.getOpenIcon());
        jTree1.setCellRenderer(renderer);

    }

    private void getDriveName() {
        DefaultMutableTreeNode myComputer = new DefaultMutableTreeNode("My Computer");
        File[] drivers = File.listRoots();
        for (File d : drivers) {
            String dName = FileSystemView.getFileSystemView().getSystemDisplayName(d);
            if ("".equals(dName)) {
                dName = "CD Drive";
            }
            myComputer.add(new DefaultMutableTreeNode(dName));
        }
        jTree1.setModel(new javax.swing.tree.DefaultTreeModel(myComputer));
    }

    private void setTable() {
        modelTable = new DefaultTableModel(null, new String[]{"", "Name", "Size", "Date Modified"}) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        jTable1.setModel(modelTable);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane2 = new javax.swing.JSplitPane();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        btnBack = new javax.swing.JButton();
        btnForward = new javax.swing.JButton();
        txtAddress = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));

        jSplitPane2.setDividerLocation(200);
        jSplitPane2.setDividerSize(2);

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("My Computer");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("D:\\");
            treeNode1.add(treeNode2);
            jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
            jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
                public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                    jTree1ValueChanged(evt);
                }
            });
            jScrollPane1.setViewportView(jTree1);

            javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
            jPanel3.setLayout(jPanel3Layout);
            jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
            );
            jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE)
            );

            jSplitPane2.setLeftComponent(jPanel3);

            jPanel4.setBackground(new java.awt.Color(255, 255, 255));

            jScrollPane2.setBackground(new java.awt.Color(255, 255, 255));

            jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                    {null, "Hoa", null, "10/12"},
                    {null, "dai.txt", "10Kb", "3984932"}
                },
                new String [] {
                    "", "Name", "Size", "Date Modified"
                }
            ) {
                Class[] types = new Class [] {
                    java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
                };
                boolean[] canEdit = new boolean [] {
                    false, false, false, false
                };

                public Class getColumnClass(int columnIndex) {
                    return types [columnIndex];
                }

                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit [columnIndex];
                }
            });
            jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
            jTable1.setGridColor(new java.awt.Color(255, 255, 255));
            jTable1.setRowHeight(20);
            jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mousePressed(java.awt.event.MouseEvent evt) {
                    jTable1MousePressed(evt);
                }
            });
            jScrollPane2.setViewportView(jTable1);

            javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
            jPanel4.setLayout(jPanel4Layout);
            jPanel4Layout.setHorizontalGroup(
                jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)
            );
            jPanel4Layout.setVerticalGroup(
                jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE)
            );

            jSplitPane2.setRightComponent(jPanel4);

            btnBack.setText("<<");
            btnBack.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnBackActionPerformed(evt);
                }
            });

            btnForward.setText(">>");

            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnBack)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnForward)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
                        .addContainerGap()))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnBack)
                            .addComponent(btnForward)
                            .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
            );

            jMenu1.setText("File");
            jMenuBar1.add(jMenu1);

            jMenu2.setText("Edit");
            jMenuBar1.add(jMenu2);

            setJMenuBar(jMenuBar1);

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jSplitPane2)
                .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jSplitPane2))
            );

            pack();
        }// </editor-fold>//GEN-END:initComponents

    private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
        // TODO add your handling code here:
        TreePath path = evt.getPath();
        Object[] dd = path.getPath();
        String x = "";
        for (Object o : dd) {
            x += o.toString() + "\\";
        }
        try {
            x = x.substring(12, x.length());
            int begin = x.indexOf(":)");
            x = x.substring(begin - 1, begin + 1) + x.substring(begin + 2);
            getChildNode(x);
            getDetailRight(x);
            jTree1.expandPath(path);
        } catch (Exception e1) {
        }
        //System.out.println(x);
    }//GEN-LAST:event_jTree1ValueChanged

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        String path = txtAddress.getText();
        path = path.substring(0, path.length() - 1);
        path = path.substring(0, path.lastIndexOf("\\") + 1);
        txtAddress.setText(path);
        getDetailRight(path);
    }//GEN-LAST:event_btnBackActionPerformed

    private void jTable1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MousePressed
        // TODO add your handling code here:
        if (evt.getClickCount() == 2) {
            int row = jTable1.getSelectionModel().getLeadSelectionIndex();
            int col = jTable1.getColumnModel().getSelectionModel().getLeadSelectionIndex();
            String path = jTable1.getModel().getValueAt(row, col).toString();
            path = txtAddress.getText() + path + "\\";
            File f = new File(path);
            if (f.isDirectory()) {
                getDetailRight(path);
            } else if (f.isFile()) {
                try {
                    Desktop.getDesktop().open(f);
                } catch (IOException ex) {

                }
            }
        }
    }//GEN-LAST:event_jTable1MousePressed

    void getChildNode(String path) {
        DefaultMutableTreeNode top = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
        if (top == null) //Nothing is selected.     
        {
            return;
        }
        File f = new File(path);
        for (File fn : f.listFiles()) {
            if (fn.isDirectory()) {
                DefaultMutableTreeNode child = new DefaultMutableTreeNode(fn.getName());

                top.add(child);
            }
        }
    }

    private void getDetailRight(String path) {
        txtAddress.setText(path);
        for (int i = 0; i < modelTable.getRowCount();) {
            modelTable.removeRow(0);
        }
        File f = new File(path);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        for (File fn : f.listFiles()) {
            if (fn.isDirectory()) {
                String[] row = {"", fn.getName(), "", sdf.format(fn.lastModified())};
                modelTable.insertRow(modelTable.getRowCount(), row);
            }
        }
        for (File fn : f.listFiles()) {
            if (fn.isFile()) {
                String[] row = {"", fn.getName(), fn.length() + " KB", sdf.format(fn.lastModified())};
                modelTable.insertRow(modelTable.getRowCount(), row);
            }
        }
    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            // Set cross-platform Java L&F (also called "Metal")
            UIManager.setLookAndFeel(
                    UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            // handle exception
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnForward;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTree jTree1;
    private javax.swing.JTextField txtAddress;
    // End of variables declaration//GEN-END:variables
}
